package c.c.b.b.f;

import c.c.b.b.e.c;
import c.c.b.b.e.f;
import c.c.b.b.i.b;
import c.c.b.b.i.d;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

/* compiled from: CacheDispatcher */
public class h extends Thread {
    public static final boolean g = q.a;
    public final BlockingQueue<c<?>> a;
    public final BlockingQueue<c<?>> b;
    public final b c;
    public final d d;
    public volatile boolean e = false;
    public final a f;

    /* compiled from: CacheDispatcher */
    public static class a implements c.b {
        public final Map<String, List<c<?>>> a = new HashMap();
        public final h b;

        public a(h hVar) {
            this.b = hVar;
        }

        /* JADX WARNING: Missing block: B:20:?, code skipped:
            return false;
     */
        /* JADX WARNING: Missing block: B:21:?, code skipped:
            return true;
     */
        public static boolean b(c.c.b.b.f.h.a r5, c.c.b.b.f.c r6) {
            /*
            monitor-enter(r5);
            r0 = r6.getCacheKey();	 Catch:{ all -> 0x0053 }
            r1 = r5.a;	 Catch:{ all -> 0x0053 }
            r1 = r1.containsKey(r0);	 Catch:{ all -> 0x0053 }
            r2 = 1;
            r3 = 0;
            if (r1 == 0) goto L_0x003a;
        L_0x000f:
            r1 = r5.a;	 Catch:{ all -> 0x0053 }
            r1 = r1.get(r0);	 Catch:{ all -> 0x0053 }
            r1 = (java.util.List) r1;	 Catch:{ all -> 0x0053 }
            if (r1 != 0) goto L_0x001e;
        L_0x0019:
            r1 = new java.util.ArrayList;	 Catch:{ all -> 0x0053 }
            r1.<init>();	 Catch:{ all -> 0x0053 }
        L_0x001e:
            r4 = "waiting-for-response";
            r6.addMarker(r4);	 Catch:{ all -> 0x0053 }
            r1.add(r6);	 Catch:{ all -> 0x0053 }
            r6 = r5.a;	 Catch:{ all -> 0x0053 }
            r6.put(r0, r1);	 Catch:{ all -> 0x0053 }
            r6 = c.c.b.b.f.q.a;	 Catch:{ all -> 0x0053 }
            if (r6 == 0) goto L_0x0038;
        L_0x002f:
            r6 = "Request for cacheKey=%s is in flight, putting on hold.";
            r1 = new java.lang.Object[r2];	 Catch:{ all -> 0x0053 }
            r1[r3] = r0;	 Catch:{ all -> 0x0053 }
            c.c.b.b.f.q.c(r6, r1);	 Catch:{ all -> 0x0053 }
        L_0x0038:
            monitor-exit(r5);
            goto L_0x0052;
        L_0x003a:
            r1 = r5.a;	 Catch:{ all -> 0x0053 }
            r4 = 0;
            r1.put(r0, r4);	 Catch:{ all -> 0x0053 }
            r6.a(r5);	 Catch:{ all -> 0x0053 }
            r6 = c.c.b.b.f.q.a;	 Catch:{ all -> 0x0053 }
            if (r6 == 0) goto L_0x0050;
        L_0x0047:
            r6 = "new request, sending to network %s";
            r1 = new java.lang.Object[r2];	 Catch:{ all -> 0x0053 }
            r1[r3] = r0;	 Catch:{ all -> 0x0053 }
            c.c.b.b.f.q.c(r6, r1);	 Catch:{ all -> 0x0053 }
        L_0x0050:
            monitor-exit(r5);
            r2 = 0;
        L_0x0052:
            return r2;
        L_0x0053:
            r6 = move-exception;
            monitor-exit(r5);
            throw r6;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.c.b.b.f.h$a.b(c.c.b.b.f.h$a, c.c.b.b.f.c):boolean");
        }

        public synchronized void a(c<?> cVar) {
            String cacheKey = cVar.getCacheKey();
            List list = (List) this.a.remove(cacheKey);
            if (!(list == null || list.isEmpty())) {
                if (q.a) {
                    q.a("%d waiting requests for cacheKey=%s; resend to network", Integer.valueOf(list.size()), cacheKey);
                }
                c cVar2 = (c) list.remove(0);
                this.a.put(cacheKey, list);
                cVar2.a((c.b) this);
                try {
                    this.b.b.put(cVar2);
                } catch (InterruptedException e) {
                    q.d("Couldn't add request to queue. %s", e.toString());
                    Thread.currentThread().interrupt();
                    h hVar = this.b;
                    hVar.e = true;
                    hVar.interrupt();
                }
            }
            return;
        }
    }

    public h(BlockingQueue<c<?>> blockingQueue, BlockingQueue<c<?>> blockingQueue2, b bVar, d dVar) {
        this.a = blockingQueue;
        this.b = blockingQueue2;
        this.c = bVar;
        this.d = dVar;
        this.f = new a(this);
    }

    public final void a() throws InterruptedException {
        c cVar = (c) this.a.take();
        cVar.addMarker("cache-queue-take");
        cVar.a(1);
        try {
            if (cVar.isCanceled()) {
                cVar.a("cache-discard-canceled");
            } else {
                c.c.b.b.i.b.a b = ((j) this.c).b(cVar.getCacheKey());
                if (b == null) {
                    cVar.addMarker("cache-miss");
                    if (!a.b(this.f, cVar)) {
                        this.b.put(cVar);
                    }
                } else {
                    if ((b.f < System.currentTimeMillis() ? 1 : null) != null) {
                        cVar.addMarker("cache-hit-expired");
                        cVar.setCacheEntry(b);
                        if (!a.b(this.f, cVar)) {
                            this.b.put(cVar);
                        }
                    } else {
                        cVar.addMarker("cache-hit");
                        p a = cVar.a(new m(b.b, b.h));
                        cVar.addMarker("cache-hit-parsed");
                        k kVar;
                        c cVar2;
                        if ((b.g < System.currentTimeMillis() ? 1 : null) == null) {
                            kVar = (k) this.d;
                            kVar.a(cVar, a, null);
                            cVar2 = kVar.c;
                            if (cVar2 != null) {
                                ((f) cVar2).c(cVar, a);
                            }
                        } else {
                            cVar.addMarker("cache-hit-refresh-needed");
                            cVar.setCacheEntry(b);
                            a.d = true;
                            if (a.b(this.f, cVar)) {
                                kVar = (k) this.d;
                                kVar.a(cVar, a, null);
                                cVar2 = kVar.c;
                                if (cVar2 != null) {
                                    ((f) cVar2).c(cVar, a);
                                }
                            } else {
                                ((k) this.d).a(cVar, a, new g(this, cVar));
                            }
                        }
                    }
                }
            }
        } catch (Throwable th) {
            cVar.a(2);
        }
        cVar.a(2);
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:24:0x0066 */
    /* JADX WARNING: Missing exception handler attribute for start block: B:26:0x006a */
    /* JADX WARNING: Can't wrap try/catch for region: R(11:19|20|21|22|23|24|25|26|27|43|28) */
    /* JADX WARNING: Can't wrap try/catch for region: R(11:19|20|21|22|23|24|25|26|27|43|28) */
    public void run() {
        /*
        r11 = this;
        r0 = g;
        r1 = 0;
        if (r0 == 0) goto L_0x000c;
    L_0x0005:
        r0 = "start new dispatcher";
        r2 = new java.lang.Object[r1];
        c.c.b.b.f.q.a(r0, r2);
    L_0x000c:
        r0 = 10;
        android.os.Process.setThreadPriority(r0);
        r0 = r11.c;
        r0 = (c.c.b.b.f.j) r0;
        monitor-enter(r0);
        r2 = r0.c;	 Catch:{ all -> 0x0089 }
        r2 = r2.exists();	 Catch:{ all -> 0x0089 }
        if (r2 != 0) goto L_0x0038;
    L_0x001e:
        r2 = r0.c;	 Catch:{ all -> 0x0089 }
        r2 = r2.mkdirs();	 Catch:{ all -> 0x0089 }
        if (r2 != 0) goto L_0x0036;
    L_0x0026:
        r2 = "Unable to create cache dir %s";
        r3 = 1;
        r3 = new java.lang.Object[r3];	 Catch:{ all -> 0x0089 }
        r4 = r0.c;	 Catch:{ all -> 0x0089 }
        r4 = r4.getAbsolutePath();	 Catch:{ all -> 0x0089 }
        r3[r1] = r4;	 Catch:{ all -> 0x0089 }
        c.c.b.b.f.q.d(r2, r3);	 Catch:{ all -> 0x0089 }
    L_0x0036:
        monitor-exit(r0);
        goto L_0x0071;
    L_0x0038:
        r2 = r0.c;	 Catch:{ all -> 0x0089 }
        r2 = r2.listFiles();	 Catch:{ all -> 0x0089 }
        if (r2 != 0) goto L_0x0042;
    L_0x0040:
        monitor-exit(r0);
        goto L_0x0071;
    L_0x0042:
        r3 = r2.length;	 Catch:{ all -> 0x0089 }
        r4 = 0;
    L_0x0044:
        if (r4 >= r3) goto L_0x0070;
    L_0x0046:
        r5 = r2[r4];	 Catch:{ all -> 0x0089 }
        r6 = r5.length();	 Catch:{ all -> 0x006a }
        r8 = new c.c.b.b.f.j$b;	 Catch:{ all -> 0x006a }
        r9 = new java.io.BufferedInputStream;	 Catch:{ all -> 0x006a }
        r10 = new java.io.FileInputStream;	 Catch:{ all -> 0x006a }
        r10.<init>(r5);	 Catch:{ all -> 0x006a }
        r9.<init>(r10);	 Catch:{ all -> 0x006a }
        r8.m1017init(r9, r6);	 Catch:{ all -> 0x006a }
        r9 = c.c.b.b.f.j.a.a(r8);	 Catch:{ all -> 0x0066 }
        r9.a = r6;	 Catch:{ all -> 0x0066 }
        r6 = r9.b;	 Catch:{ all -> 0x0066 }
        r0.g(r6, r9);	 Catch:{ all -> 0x0066 }
    L_0x0066:
        r8.close();	 Catch:{ all -> 0x006a }
        goto L_0x006d;
    L_0x006a:
        r5.delete();	 Catch:{ all -> 0x0089 }
    L_0x006d:
        r4 = r4 + 1;
        goto L_0x0044;
    L_0x0070:
        monitor-exit(r0);
    L_0x0071:
        r11.a();	 Catch:{ InterruptedException -> 0x0075 }
        goto L_0x0071;
    L_0x0075:
        r0 = r11.e;
        if (r0 == 0) goto L_0x0081;
    L_0x0079:
        r0 = java.lang.Thread.currentThread();
        r0.interrupt();
        return;
    L_0x0081:
        r0 = "Ignoring spurious interrupt of CacheDispatcher thread; use quit() to terminate it";
        r2 = new java.lang.Object[r1];
        c.c.b.b.f.q.d(r0, r2);
        goto L_0x0071;
    L_0x0089:
        r1 = move-exception;
        monitor-exit(r0);
        throw r1;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.b.b.f.h.run():void");
    }
}
